
#include "Warnning.h"
#include <qtimer.h>
#include "assert.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <signal.h>




Warnning::Warnning(int ledId) : m_fd(-1), m_ledId(ledId), m_isOn(false)
{
	m_timer = new QTimer(this);
	connect(m_timer, SIGNAL(timeout()), this, SLOT(startWarnning()));
	m_fd = open("/dev/car_led", 0);
	printf("m_fd = %d\n", m_fd);
	if (m_fd < 0)
	{
		assert(false);

	}


}

Warnning::~Warnning()
{
	m_timer->stop();
	delete m_timer;
	close(m_fd);
}

void Warnning::update(const SensorState& state)
{
	(void)state;

}


void Warnning::toWarnning()
{


	printf("cccc\n");
	m_timer->start(100);
}

void Warnning::startWarnning()
{

	if(m_isOn)
	{
		ledOff();
		m_isOn = false;
	}
	else
	{
		ledOn();
		m_isOn = true;
	}
	if(checkIsCancel())
	{
		m_timer->stop();
		emit cancelWarnning();
	}

}



void Warnning::ledOn()
{
	assert(m_fd > 0);
	ioctl(m_fd, 1, m_ledId);
}
void Warnning::ledOff()
{
	assert(m_fd > 0);
	ioctl(m_fd, 0, m_ledId);
}



bool Warnning::checkIsCancel()
{
	return false;
}
